package AionCrafter.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import AionCrafter.AionCrafterApplication;

public class Recipe {
	private String name;
	private int quantityProduced;
	private int craftLevel;
	private String craftProfession;
	private String race;
	private Map<String,Integer> components; //Id,Quantity
	
	public Recipe(String n, int q, int cLevel, String p, String r, Map<String,Integer> c){
		name = n;
		quantityProduced = q;
		craftLevel = cLevel;
		craftProfession = p;
		race = r;
		components = c;
	}
	
	public void setName(String n){
		name = n;
	}
	
	public String getName(){
		return name;
	}
	
	public void setLevel(int l){
		craftLevel = l;
	}
	
	public int getLevel(){
		return craftLevel;
	}
	
	private void addToQuantityMap(Map<String,Integer> quantityMap, Map.Entry<String, Integer> pair, int numOfCrafts){
		if(quantityMap.containsKey(pair.getKey())){
			int val = quantityMap.get(pair.getKey());
			quantityMap.remove(pair.getKey());
			quantityMap.put(pair.getKey(), pair.getValue()*numOfCrafts + val);
		} else {
			quantityMap.put(pair.getKey(), pair.getValue()*numOfCrafts);
		}
	}
	
	public String getSubcomponentDisplay(int numOfCrafts, Map<String,Recipe> recipeMap, int depth, Map<String,Integer> quantityMap){
		String scDisplay = "";
		Iterator itr = components.entrySet().iterator();
		while(itr.hasNext()){
			Map.Entry<String,Integer> pairs = (Map.Entry<String,Integer>) itr.next();
			addToQuantityMap(quantityMap, pairs, numOfCrafts);
			Recipe component = recipeMap.get(pairs.getKey());
			if(component != null){
				for(int i = 0; i < depth; i++){scDisplay+='-';}
				scDisplay += AionCrafterApplication.materialList.getMaterials().get(pairs.getKey()).getName() 
				+ '(' + numOfCrafts*pairs.getValue() + ')' + "\n";
				scDisplay += component.getSubcomponentDisplay(pairs.getValue() * numOfCrafts, recipeMap, depth+1, quantityMap);
			} else {
				for(int i = 0; i < depth; i++){scDisplay+='-';}
				scDisplay += AionCrafterApplication.materialList.getMaterials().get(pairs.getKey()).getName() 
				+ '(' + numOfCrafts*pairs.getValue() + ')' + "\n";
			}
		}
		
		return scDisplay;
	}
}
